package content

import (
	"fmt"
	"woo-server/coms/structs"
	"woo-server/coms/utils"
	"woo-server/models/db"
)

// 文章字段表
type Articles struct {
	*structs.Common `gorm:"-" json:"-"`
	ArticleId       uint64          `gorm:"primary_key;AUTO_INCREMENT;column:article_id;"` // 自增主键
	ArticleTitle    string          `gorm:"column:article_title;size:255;"`                // 文章标题
	ShowType        uint64          `gorm:"column:show_type;size:255;"`                    // 列表显示方式(10小图展示 20大图展示)
	CategoryId      uint64          `gorm:"column:category_id;size:255;"`                  // 文章分类id
	FileId          uint64          `gorm:"column:image_id;size:50;"`                      // 封面图id
	ArticleContent  string          `gorm:"column:article_content;"`                       // 文章内容
	Sort            uint            `gorm:"column:article_sort;size:255;"`                 // 文章排序(数字越小越靠前)
	Status          uint            `gorm:"column:article_status;"`                        // 文章状态(0隐藏 1显示)
	VirtualViews    uint            `gorm:"column:virtual_views;size:255;"`                // 虚拟阅读量(仅用作展示)
	ActualViews     uint            `gorm:"column:actual_views;size:255;"`                 // 实际阅读量
	IsDelete        int             `gorm:"column:is_delete;size:150;"`                    // 是否删除
	Image           []UploadFile    `gorm:"foreignKey:FileId;references:FileId"`           // 图片模型数组返回
	Category        ArticleCategory `gorm:"foreignKey:CategoryId"`                         // 分类模型返回
	db.Basics
}

// 表名
func (s *Articles) TableName() string {
	return "woo_article"
}

// 初始化
func NewArticles(com *structs.Common) *Articles {
	d := &Articles{}

	return d
}

func (s *Articles) GetConfig() structs.CFConfig {
	list := structs.CFConfig{}
	list.Column = s.Column()
	list.Forms = s.Forms()
	return list
}

func (s *Articles) Column() []structs.ColumnCommon {
	result := make([]structs.ColumnCommon, 0)
	result = append(result, structs.ColumnCommon{Title: "ID", DataIndex: "ArticleId"})
	result = append(result, structs.ColumnCommon{Title: "封面", DataIndex: "Image"})
	result = append(result, structs.ColumnCommon{Title: "标题", DataIndex: "ArticleTitle"})
	result = append(result, structs.ColumnCommon{Title: "分类", DataIndex: "Category.Name"})
	result = append(result, structs.ColumnCommon{Title: "文章类型", DataIndex: "ShowType"})
	result = append(result, structs.ColumnCommon{Title: "排序", DataIndex: "Sort"})
	result = append(result, structs.ColumnCommon{Title: "更新时间", DataIndex: "UpdateDate"})
	result = append(result, structs.ColumnCommon{Title: "操作", DataIndex: "Operation"})
	return result
}

func (s *Articles) Forms() []structs.FormsCommon {
	result := make([]structs.FormsCommon, 0)
	list := GetAllCategory()
	result = append(result, structs.FormsCommon{Key: "ArticleId", Label: "主键ID", Type: "hidden", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "CategoryId", Label: "分类", Type: "OnceSelect", Options: list})
	result = append(result, structs.FormsCommon{Key: "ArticleTitle", Label: "标题", Type: "input", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "ShowType", Label: "文章类型", Type: "radio", Options: []map[string]interface{}{{"Value": 20, "Name": "大图"}, {"Value": 10, "Name": "小图"}}})
	result = append(result, structs.FormsCommon{Key: "FileId", Label: "封面", Type: "onlyUploadImage", Options: "Image"})
	result = append(result, structs.FormsCommon{Key: "ArticleContent", Label: "内容", Type: "markdown", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "Sort", Label: "排序", Type: "number", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "ActualViews", Label: "实际阅读量", Type: "input", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "VirtualViews", Label: "虚拟阅读量", Type: "input", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "Status", Label: "状态", Type: "switch", Options: []string{}})
	return result
}

// 创建
func (s *Articles) Create() error {
	return utils.GORM.Debug().Table(s.TableName()).Create(s).Error
}

// 批量创建
func (s *Articles) BatchCreate(data []*Articles) error {
	return utils.GORM.Debug().Table(s.TableName()).Create(data).Error
}

// 获取分页列表及总数
func (s *Articles) GetListAndTotal(search string, page int, pageSize int) ([]*Articles, int64) {
	list := make([]*Articles, 0)
	var total int64 = 0
	// 查询
	query := utils.GORM.Debug().Table(s.TableName())
	query = query.Where("is_delete = 0").Order("article_sort asc")
	if len(search) > 0 {
		search = fmt.Sprintf("%%%s%%", search)
		query = query.Where("article_title = ?", search)
	}

	// 总数
	query.Count(&total)

	// 列表
	offset := (page - 1) * pageSize
	query.Preload("Image").Preload("Category").Limit(pageSize).Offset(offset).Find(&list)

	return list, total
}

// 获取单数据
func (s *Articles) GetFirst(where string, field int) *Articles {
	//info := make([]*Articles, 0)

	// 查询
	query := utils.GORM.Debug().Table(s.TableName())

	query = query.Where(where, field)

	query.Preload("Image").First(&s)
	return s
}

// 获取文章分类
func (s *Articles) GetCategory(where string, field []int) []Articles {
	list := make([]Articles, 0)

	// 查询
	query := utils.GORM.Debug().Table(s.TableName())
	query = query.Order("sort asc")
	query = query.Where(where, field)

	query.Find(&list)
	return list
}

// 更新文章
func (s *Articles) Save() error {
	// 更新
	return utils.GORM.Debug().Table(s.TableName()).Save(s).Error
}

// 删除文章
func (s *Articles) Delete() error {
	return utils.GORM.Debug().Table(s.TableName()).Delete(s).Error
}

// 删除文章
func (s *Articles) SetDelete() error {
	return utils.GORM.Debug().Table(s.TableName()).Where("article_id = ?", s.ArticleId).Update("is_delete", 1).Error
}

// 获取单条数据
func (s *Articles) First() error {
	return utils.GORM.Debug().Table(s.TableName()).First(s).Error
}
